using System;

namespace GameFrameWork
{
    public class GameSingletonBase<T> where T : class
    {
        private static T _instance;

        // private static readonly object syslock = new object();

        public static T GetInstance()
        {
            if (_instance == null)
            {
                // lock (syslock)
                {
                    if (_instance == null)
                    {
#if UNITY_EDITOR
                        UnityEngine.Profiling.Profiler.BeginSample($"GameSingletonBase_{typeof(T).Name}");
#endif
                        _instance = Activator.CreateInstance<T>();
#if UNITY_EDITOR
                        UnityEngine.Profiling.Profiler.EndSample();
#endif
                    }
                }
            }
            return _instance;
        }

        public static T Instance
        {
            get
            {

                return GetInstance();
            }
        }

        public static void Clear()
        {
            if (_instance != null)
            {
                _instance.GetType().GetMethod("Dispose")?.Invoke(_instance, null);
                _instance = null;
            }
        }

    }
}
